/**
 * Created by jiangxiaowei on 2016/12/19.
 */

'use strict';
let mongoose = require('mongoose');

let PublishLogSchema = new mongoose.Schema({
  'username':String,
  'envCode':String,
  'projectName':String,
  'version':String,
  'publishTime':String,
  'operation':String,
  'status':Boolean
});

//静态方法，只在model层使用
PublishLogSchema.statics = {

  getLogsCount:function ({envCode,projectName}, cb) {
    return this.find({envCode,projectName}).count().exec(cb);
  },

  getLastLog:function ({envCode,projectName,index}, cb) {
    return this.aggregate([{'$match':{envCode,projectName}}, {'$limit':index}, {'$sort':{'_id':-1}}, {'$limit':-1}]).exec(cb);
  },

  getLogsByPage:function ({envCode,projectName,objectId,pageSize}, cb) {
    return this.find({envCode,projectName,'_id':{'$gt':objectId}}).sort({'_id':-1}).limit(pageSize).exec(cb);
  },

  getLogsBySkip:function ({envCode,projectName,pageNum,pageSize}, cb) {
    return this.find({envCode,projectName}).sort({'_id':-1}).skip(pageNum*pageSize).limit(pageSize).exec(cb);
  }

};

module.exports = PublishLogSchema;